package com.macro.mall.common.domain;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 登录用户信息
 * Created by macro on 2020/6/19.
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class UserDto {
    private Long id;
    private String username;
    private String password;
    private Integer status;
    private String clientId;
    private List<String> roles;

    public UserDto() {
    }

    public UserDto(Long id, String username, Integer status) {
        this.id = id;
        this.username = username;
        this.status = status;
    }

    public static void main(String[] args) {
        List<UserDto> userDtos = new ArrayList<>();
        userDtos.add(new UserDto(1L, "zhangsan", 2));
        userDtos.add(new UserDto(2L, "lisi", 1));
        userDtos.add(new UserDto(3L, "wangwu", 1));
        userDtos.add(new UserDto(4L, "zhaoliu", 2));
        userDtos.forEach(temp -> System.out.println(temp.getId() + "; " + temp.getUsername() + "; " + temp.getStatus()));

        System.out.println("~~~~~~~~~~~~~~过滤器~~~~~~~~~~~~~~");
        List<UserDto> userDtos2 = userDtos.stream().filter(temp -> !temp.getStatus().equals(2)).collect(Collectors.toList());
        userDtos2.forEach(temp -> System.out.println(temp.getId() + "; " + temp.getUsername() + "; " + temp.getStatus()));

        System.out.println("~~~~~~~~~~~~~~提取对象中的ID~~~~~~~~~~~~~~");
        List<Long> ids = userDtos.stream().map(temp -> temp.getId()).collect(Collectors.toList());
        ids.forEach(temp -> System.out.println(temp));

        System.out.println("~~~~~~~~~~~~~~分页~~~~~~~~~~~~~~");
        List<UserDto> userDtos3 = userDtos.stream().limit(3).collect(Collectors.toList());
        userDtos3.forEach(temp -> System.out.println(temp.getId() + "; " + temp.getUsername() + "; " + temp.getStatus()));

        System.out.println("~~~~~~~~~~~~~~计数~~~~~~~~~~~~~~");
        System.out.println(userDtos.stream().filter(temp -> !temp.getStatus().equals(2)).count());

        System.out.println("~~~~~~~~~~~~~~排序（倒叙）~~~~~~~~~~~~~~");
        List<UserDto> userDtos4 = userDtos.stream().sorted((pre1, pre2) -> {return pre2.getStatus().compareTo(pre1.getStatus());}).collect(Collectors.toList());
        userDtos4.forEach(temp -> System.out.println(temp.getId() + "; " + temp.getUsername() + "; " + temp.getStatus()));

        System.out.println("~~~~~~~~~~~~~~list转map~~~~~~~~~~~~~~");
        Map<Long, UserDto> maps = userDtos.stream().collect(Collectors.toMap(temp -> temp.getId(), temp -> temp));
        maps.forEach((k, v) -> System.out.println(k + " = " + v));

        System.out.println("~~~~~~~~~~~~~~跳过指定个数~~~~~~~~~~~~~~");
        List<UserDto> userDtos5 = userDtos.stream().skip(1).collect(Collectors.toList());
        userDtos5.forEach(temp -> {
            if(temp.getId() == 3) {
                temp.setUsername(temp.getUsername() + "'s ID is 3.");
            }
        });
        userDtos5.forEach(temp -> System.out.println(temp.getId() + "; " + temp.getUsername() + "; " + temp.getStatus()));
    }
}
